/** 
 * @fileoverview 
 * LIVE initialization
 * 
 * 
 * 
 * @author Kelemen Viktor kelemen.viktor@gmail.com
 * @version 0.5
 * 
 */

/*        var djConfig = {
            gfxRenderer:"silverlight"        
		}; 
*/
// libraries that will be loaded
var libraries = [ 
	  "dojo/dojo",
	  //"Silverlight",
	  "dojox/gfx/matrix",
	  "dojox/gfx/arc",
	  "dojox/gfx/decompose",
	  "dojox/gfx/_base",
	 
	  "dojox/gfx/shape",
	  "dojox/gfx/path",
	  "dojox/gfx/Mover",
	  "dojox/gfx/Moveable",
	  "dojox/gfx/move",

	  "dojox/gfx/svg",
	  "dojox/gfx/svg_attach",					  
	  
	//  "dojox/gfx/canvas",
	//  "dojox/gfx/canvas_attach",
						  
	//  "dojox/gfx/silverlight",
	//  "dojox/gfx/silverlight_attach",
	  
	//  "dojox/gfx/vml",
	//  "dojox/gfx/vml_attach",

	  "dojox/gfx",
	  "dojox/gfx/utils",
	  
	  "layer",
	  "net.feed",
	  "net.page",
	  "net.flickr",
	  "math",
	  "lang.style",
	  "layout.grid",
	  "layout.spiral",
	  "interface"
];

	// loader box
var loadingbox = document.createElement('div');



var _setupLiveJS = function()	{

	window.document.body.removeChild(loadingbox);

	/**
	 * Global variables
	 */
	LAYERS = [];	
	LAYERNUM = 3;
	FRAMECOUNTER = 0;
	FRAMERATE = 24;		
	
	
	/**
	 * Screen and GFX element
	 */
	var screen = document.createElement('div');
	screen.setAttribute('id','screen');
	
	var gfxcanvas = document.createElement('div');
	gfxcanvas.setAttribute('id','gfxcanvas');

	document.body.appendChild(screen);
	document.body.appendChild(gfxcanvas);	


	/**
	 * OSC Applet
	 */
	 /*var oscclient = document.createElement('div');
	 oscclient.setAttribute('id','OSCClient_container');
	 
	 oscclient.innerHTML = '<object name="applet" id="applet" classid="java:OSCClient.class" type="application/x-java-applet" archive="http://people.inf.elte.hu/kelemen/osc/OSCClient.jar" width="1" height="1" >'+
           '<param name="archive" value="OSCClient.jar" />'+
		   '<param name="mayscript" value="true" />'+
		   '<param name="scriptable" value="true" />'+
		   '<param name="image" value="loading.gif" />'+
		   '<param name="boxmessage" value="Loading Processing software..." />'+
		   '<param name="boxbgcolor" value="#FFFFFF" />'+
		   '</object>';
	 
	 document.body.appendChild(oscclient);
*/
	/**
	 * Shortcuts
	 */
	 
	// FIXME: what if there is already $
	$ = dojo.byId;
			
	
	$$ = dojo.query;

	SCR = $('screen');
	GFX = dojox.gfx.createSurface('gfxcanvas', 1024, 768);

	// layout
	makeGrid = YAHOO.layout.grid.makeGrid;
	makeSpiral = YAHOO.layout.spiral.makeSpiral;
	
	
	// net package
	getFeed = YAHOO.net.feed.getFeed;
	getPage = YAHOO.net.page.getPage;
	getFlickr = YAHOO.net.flickr.getFlickr;

	// math package
	rnd = YAHOO.math.getRandom;
	sin = Math.sin;
	cos = Math.cos;
	PI = Math.PI;

	// log
	log = function(a) { try { console.log(a); } catch(e) { } };
	debug = function(a) { try { console.debug(a); } catch(e) { } };
	


	/**
	 * Code editor
	 */
	YAHOO.interface.createEditor();		

	/**
	 * Layers
	 */
	for(var i=0;i<LAYERNUM;i++)	{
		LAYERS.push( new YAHOO.layer(i));
	}

	LAYER0 = LAYERS[0];
	LAYER1 = LAYERS[1];
	LAYER2 = LAYERS[2];		

	

	
	log('LiveJS initialization done.')
	
	
	_drawFrame();
		
	}	


/**
 * Loads the libraries that are in the libraries[]
 * 
 */
var _loadLibraries = function()	{

	var libpath = LIVELIBS!=null?LIVELIBS:"../lib";
	
	loadingbox.setAttribute('id','livejs_loadingbox');
	loadingbox.innerHTML = "";
	window.document.body.appendChild(loadingbox,null);
	
	
	var failure = function()	{	try { console.log(libraries[i]+' cant loaded!'); } catch (e) {}; }
		
	var i=0;
	var loading = function()	{
		i++;	
   		if (i==libraries.length) {  _setupLiveJS();return; }
		    			
		loadingbox.innerHTML= libraries[i]+"<br/>"+loadingbox.innerHTML;
		
		if (i<libraries.length)	{ 
			YAHOO.util.Get.script(libpath+'/'+libraries[i]+'.js', { onSuccess: loading, onFailure: failure });
		}
	}
	
	
	YAHOO.util.Get.script(libpath+'/'+libraries[0]+'.js', { onSuccess: loading, onFailure: failure });
	YAHOO.util.Get.css(libpath+'/live.css');

}


/**
 * draws a frame and times the next
 */
var _drawFrame = function()	{
	
	FRAMECOUNTER++;

	dojo.forEach(LAYERS, function(l){
		l.draw();
	}, this);
	
	setTimeout(_drawFrame,FRAMERATE);
}



_loadLibraries();



